/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tthdhm.truyvetduongdi.servlet;

import com.tthdhm.truyvetduongdi.dao.CustomerDAO;
import com.tthdhm.truyvetduongdi.dao.TelephoneInfoDAO;
import com.tthdhm.truyvetduongdi.dao.VictimInfoDAO;
import com.tthdhm.truyvetduongdi.pojo.CustomerPOJO;
import com.tthdhm.truyvetduongdi.pojo.TelephoneInfoPOJO;
import com.tthdhm.truyvetduongdi.pojo.VictimInfoPOJO;
import com.tthdhm.truyvetduongdi.util.Utils;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author BPT
 */
public class CustomerServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");
        if (action != null) {
            if (action.equals("Login")) {
                String username = request.getParameter("txtUsername");
                String password = request.getParameter("txtPassword");
                if (CustomerDAO.getInstance().getCustomer(username, Utils.getInstance().doHashString(password)) != null) {
                    request.getSession().setAttribute("Login", 1);
                    request.getSession().setAttribute("Username", username);
                    List<VictimInfoPOJO> listVictimInfo = VictimInfoDAO.getInstance().getListVictimInfoByUsername(username);
                    request.getSession().setAttribute("ListID", listVictimInfo);
                    response.sendRedirect(request.getContextPath());
                } else {
                    response.sendRedirect(request.getContextPath());
                }
                return;
            }
            if (action.equals("Logout")) {
                request.getSession().removeAttribute("Login");
                request.getSession().removeAttribute("Username");
                response.sendRedirect(request.getContextPath());
                return;
            }
            if (action.equals("LoginMobile")) {
                String username = request.getParameter("txtUsername");
                String password = request.getParameter("txtPassword");
                if (CustomerDAO.getInstance().getCustomer(username, Utils.getInstance().doHashString(password)) != null) {
                    request.getSession().setAttribute("Login", 1);
                    request.getSession().setAttribute("Username", username);
                    List<VictimInfoPOJO> listVictimInfo = VictimInfoDAO.getInstance().getListVictimInfoByUsername(username);
                    request.getSession().setAttribute("ListID", listVictimInfo);
                    response.sendRedirect(request.getContextPath()+"/Mobile");
                } else {
                    response.sendRedirect(request.getContextPath()+"/Mobile");
                }
                return;
            }
            if (action.equals("CustomerInfo")) {
                Object username = request.getSession().getAttribute("Username");
                if (username == null) {
                    return;
                } else {
                    CustomerPOJO customer = CustomerDAO.getInstance().getCustomer(username.toString());
                    try {
                        out.println("<table style='width:400px; height:400px;'>");
                        out.println("<tr>");
                        out.println("<td class='Label'> Họ và tên</td>");
                        out.println("<td>");
                        out.println("<input type='text' name='txtFullname' id='txtFullname' class='Textbox' value='" + customer.getFullname() + "' disabled='disabled'/>");
                        out.println("</td>");
                        out.println("</tr>");
                        out.println("<tr>");
                        out.println("<td class='Label'>Ngày sinh</td>");
                        out.println("<td>");
                        out.println("<input type='text' name='txtBirthday' id='txtBirthday' class='Textbox' value='" + customer.getBirthday() + "' disabled='disabled'/>");
                        out.println("</td>");
                        out.println("</tr>");
                        out.println("<tr>");
                        out.println("<td class='Label'> Email</td>");
                        out.println("<td>");
                        out.println("<input type='text' name='txtEmail' id='txtEmail' class='Textbox' value='" + customer.getEmail() + "' disabled='disabled'/>");
                        out.println("</td>");
                        out.println("</tr>");
                        out.println("<tr>");
                        out.println("<td class='Label'> Giới tính</td>");
                        out.println("<td>");
                        out.println("<select name='txtSex' id='txtSex' disabled='disabled' class='ListBox'>");
                        if (customer.getSex() == 1) {

                            out.println("<option value='1' selected='true' >Nam</option>");
                            out.println("<option value='0'>Nữ</option>");

                        } else {
                            out.println("<option value='1'>Nam</option>");
                            out.println("<option value='0' selected='true'>Nữ</option>");
                        }
                        out.println("</select>");
                        out.println("</td>");
                        out.println("</tr>");
                        out.println("<tr>");
                        out.println("<td class='Label'>Địa chỉ</td>");
                        out.println("<td>");
                        out.println("<input type='text' name='txtAddress' id='txtAddress' class='Textbox' value='" + customer.getAddress() + "' disabled='disabled'/>");
                        out.println("</td>");
                        out.println("</tr>");
                        out.println("<tr>");
                        out.println("<td class='Label'>Điện thoại</td>");
                        out.println("<td>");
                        out.println("<input type='text' name='txtTel' id='txtTel' class='Textbox' value='" + customer.getTelephone() + "' disabled='disabled'/>");
                        out.println("</td>");
                        out.println("</tr>");
                        out.println("<tr>");
                        out.println("<td class='Label'></td>");
                        out.println("<td align='center'>");
                        out.println("<input type='checkbox' name='txtChange' id='txtChange' onchange='changeEnable()'/>Thay đổi");
                        out.println("</td>");
                        out.println("</tr>");
                        out.println("<tr>");
                        out.println("<td class='Label'></td>");
                        out.println("<td align='center'>");
                        out.println("<input type='button' name='btSave' id='btSave' class='button' value='Lưu'/>");
                        out.println("</td>");
                        out.println("</tr>");
                        out.println("</table>");
                    } catch (Exception ex) {
                        out.close();
                    } finally {
                        out.close();
                    }
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
